package com.ehsy.statement;

import com.alibaba.fastjson.JSON;
import com.dtflys.forest.http.ForestQueryMap;
import com.ehsy.common.ResponseVo;
import com.ehsy.model.MyHeaders;
import com.ehsy.service.HeadersService;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

/**
 * @author YanZhen_Li
 * @version 1.0
 * @title A4请求头
 * @description
 * @create 2024/6/17 16:22
 */
@SpringBootTest
public class A4请求头配置 {
    @Resource
    private HeadersService service;

    @Test
    public void test001(){
        ResponseVo<Object> test01 = service.test01();
        ResponseVo<Object> test02 = service.test02("Jerry_Li");
        ResponseVo<Object> test03 = service.test03("Jerry_Li");
        Map<String, Object> heards = new HashMap<>();
        heards.put("name", "Jerry_Li");
        heards.put("age", "18");
        heards.put("sex", "man");
        ResponseVo<Object> test04 = service.test04(heards);
        ResponseVo<Object> test05 = service.test05(new MyHeaders("Tom_Cat", "5", "men"));
        // 利用延迟请求参数模拟设置token
        ResponseVo<Map<String, Object>> test06 = service.test06("Tom", "Jerry", (req) -> {
            return  UUID.randomUUID().toString();
        });
        // 加密请求(模拟加密)
        // ! 注意1：这里的模拟加密，实际开发中，应该使用加密工具进行加密
        // ! 注意2：我们的延迟请求可以添加在请求头中，如果和我一样添加在了同一个地方，我们需要注意不要获取延迟请求参数，否则可能会递归死循环直至栈溢出
        ResponseVo<Map<String, Object>> test07 = service.test07("Tom", "Jerry", (req) -> {
            ForestQueryMap query = req.getQuery();
            StringBuilder builder = new StringBuilder();
            Set<String> keySet = query.keySet();
            keySet.remove("encryption");
            for (String k : keySet) {
                builder.append(k).append(">>>").append(query.get(k)).append(";");
            }
            return builder.toString();
        });
        System.out.println(test01);
        System.out.println(test02);
        System.out.println(test03);
        System.out.println(test04);
        System.out.println(test05);
        System.out.println(test06);
        System.out.println(test07);
    }
}
